package com.hjc.controller;

import com.hjc.entity.*;
import com.hjc.page.Page;
import com.hjc.service.InStoreService;
import com.hjc.service.PurchaseService;
import com.hjc.service.StoreService;
import com.hjc.utils.TokenUtils;
import com.hjc.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private InStoreService inStoreService;

    @RequestMapping("/purchase-add")
    public Result addPurchase(@RequestBody Purchase purchase) {
        return purchaseService.addPurchase(purchase);
    }

    //    http://localhost:3000/api/purchase/store-list?_t=1697446143331
    @RequestMapping("/store-list")
    public Result findStoreList() {
        List<Store> storeList = storeService.findAllStore();
        return Result.ok(storeList);
    }

    //    http://localhost:3000/api/purchase/purchase-page-list?
    //    storeId=1&startTime=&endTime=&productName=&buyUser=&isIn=&pageSize=5&pageNum=1&totalNum=0
    @RequestMapping("/purchase-page-list")
    public Result findStoreByPage(Purchase purchase, Page page) {
        Page purchasePage = purchaseService.findPurchaseByPage(purchase, page);
        return Result.ok(purchasePage);
    }

//    http://localhost:3000/api/purchase/purchase-delete/42
    @RequestMapping("/purchase-delete/{buyId}")
    public Result removePurchaseById(@PathVariable Integer buyId) {
        return purchaseService.removePurchaseById(buyId);
    }

//    http://localhost:3000/api/purchase/purchase-update
    @RequestMapping("/purchase-update")
    public Result setPurchaseById(@RequestBody Purchase purchase) {
        return purchaseService.setPurchaseById(purchase);
    }

//    http://localhost:3000/api/purchase/in-warehouse-record-add
    @RequestMapping("/in-warehouse-record-add")
    public Result addInStore(@RequestBody Purchase purchase,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

//        创建InStore对象封装信息
        InStore inStore = new InStore();
        inStore.setStoreId(purchase.getStoreId());
        inStore.setProductId(purchase.getProductId());
        inStore.setInNum(purchase.getFactBuyNum());
        inStore.setCreateBy(currentUser.getUserId());

        return inStoreService.addInStore(inStore, purchase.getBuyId());
    }


}
